package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.MStoreInExcution;
import com.fortune.dto.in.mStoreIn.MStoreInAddDto;
import com.fortune.dto.in.mStoreIn.MStoreInEditDto;
import com.fortune.dto.in.mStoreIn.MStoreInListDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.MStoreInEnum;
import com.fortune.service.MStoreInService;
import com.fortune.vo.mStoreIn.MStoreInInfo;
import com.fortune.vo.mStoreIn.MStoreInInfoDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "mStoreIn", description = "厂商仓库入库", tags = {"厂商仓库入库"})
@RestController
@RequestMapping("mStoreIn")
public class MStoreInController {
	@Autowired
	private MStoreInService mStoreInService;
	
	@ApiOperation(value = "入库单列表", nickname = "mStoreInList", notes = "", tags={ "@石匡" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
			@ApiImplicitParam(name="search", value="搜索关键词",paramType="query",dataType="string"),
			@ApiImplicitParam(name="type",value="是否已验收（0已验收1未验收)",paramType="query",dataType="Integer",allowableValues = "0,1"),
			@ApiImplicitParam(name="order", value="排序依据",paramType="query",dataType="string"),
			@ApiImplicitParam(name="types", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<MStoreInInfo>> list(HttpServletRequest request,@ApiIgnore @Validated MStoreInListDto mStoreInListDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		mStoreInListDto.setPage((mStoreInListDto.getPage()-1)*10);
		List<MStoreInInfo> list = mStoreInService.queryMStoreInList(companyId,mStoreInListDto);
		int count = mStoreInService.queryMStoreInListCount(companyId,mStoreInListDto);
		PagingData<MStoreInInfo> pagingData = new PagingData<>();
		pagingData.setData(list);
		pagingData.setRecordsTotal(count);
		return new MStoreInExcution<PagingData<MStoreInInfo>>(pagingData, MStoreInEnum.SUCCESS);
	}
	
	@ApiOperation(value = "入库单详情", nickname = "mStoreInDetail", notes = "", tags={ "@石匡" })
	@ApiImplicitParam(name="id", value="入库单id",paramType="query",dataType="string",required=true)
    @GetMapping(value = "detail",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<MStoreInInfoDetail> detail(HttpServletRequest request,@Validated @NotBlank(message="入库单id不能为空")String id) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		MStoreInInfoDetail mStoreInInfoDetail = mStoreInService.queryMStoreInInfoDetail(companyId,id);
		return new MStoreInExcution<MStoreInInfoDetail>(mStoreInInfoDetail, MStoreInEnum.SUCCESS);
	}
	
	@ApiOperation(value = "添加入库单", nickname = "mStoreInAdd", notes = "", tags={ "@石匡" })
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody MStoreInAddDto addDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		String userId = mUserInfo.getId();
		int num = mStoreInService.queryMStoreInIsExist(addDto.getNum(),companyId);
		if (num > 0){
			return new MStoreInExcution<Void>(null, MStoreInEnum.EXIST);
		}
		num = mStoreInService.addMStoreIn(userId,companyId,addDto);
		if (num < 1){
			return new MStoreInExcution<Void>(null, MStoreInEnum.FAILURE);
		}
		return new MStoreInExcution<Void>(null, MStoreInEnum.SUCCESS);
	}
	
	@ApiOperation(value = "编辑入库单", nickname = "mStoreInEdit", notes = "", tags={ "@石匡" })
    @PutMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody MStoreInEditDto editDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = mStoreInService.queryEditIsExist(editDto,companyId);
		if (num > 0){
			return new MStoreInExcution<Void>(null, MStoreInEnum.EXIST);
		}
		num = mStoreInService.editMStoreIn(companyId,editDto);
		if (num < 1){
			return new MStoreInExcution<Void>(null, MStoreInEnum.FAILURE);
		}
		return new MStoreInExcution<Void>(null, MStoreInEnum.SUCCESS);
	}
	
	@ApiOperation(value = "删除入库单", nickname = "mStoreInDelete", notes = "", tags={ "@石匡" })
	@ApiImplicitParam(name="id", value="入库单id",paramType="query",dataType="string",required=true)
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="入库单id不能为空")String id) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = mStoreInService.deleteMStoreIn(companyId,id);
		if (num < 1){
			return new MStoreInExcution<Void>(null, MStoreInEnum.FAILURE);
		}
		return new MStoreInExcution<Void>(null, MStoreInEnum.SUCCESS);
	}
	
	@ApiOperation(value = "验收入库", nickname = "mStoreInConfirm", notes = "厂商商品库存明细账中流水号不清楚是什么,目前设置为0,出库单价不确定,目前为0", tags={ "@石匡" })
	@ApiImplicitParam(name="id", value="入库单id",paramType="query",dataType="string",required=true)
    @PutMapping(value = "confirm",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> confirm(HttpServletRequest request,@Validated @NotBlank(message="入库单id不能为空")String id) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		String userId = mUserInfo.getId();
		int num = mStoreInService.updateMStoreInType(companyId,id,userId);
		if (num < 1){
			return new MStoreInExcution<Void>(null, MStoreInEnum.FAILURE);
		}
		return new MStoreInExcution<Void>(null, MStoreInEnum.SUCCESS);
	}
}
